﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kkc.Model;

namespace Kkc.Application.Controllers
{
    [HandleError]
    public class PageController: Controller
    {
        public ActionResult View(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name");

            using (var entities = new KkcDataEntities())
            {
                var pageQuery = from p in entities.Pages
                                where p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
                                select p;

                var page = pageQuery.FirstOrDefault();

                if (page == null)
                {
                    page = new Page
                    {
                        Name = name,
                        Title = name,
                        Contents = ""
                    };
                }

                ViewData.Model = page;

                return View();
            }
        }

        [Authorize(Roles = "Contributor")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name");

            using (var entities = new KkcDataEntities())
            {
                var pageQuery = from p in entities.Pages
                                where p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
                                select p;

                var page = pageQuery.FirstOrDefault();

                ViewData.Model = page;
            }
            
            return View();
        }

        [Authorize(Roles = "Contributor")]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string name, string title, string contents)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name");

            using (var entities = new KkcDataEntities())
            {
                var pageQuery = from p in entities.Pages
                                where p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
                                select p;

                var page = pageQuery.First();
                page.Title = title;
                page.Contents = contents;
                entities.SaveChanges();

                return RedirectToAction("View", new { name = name });
            }
        }

        [Authorize(Roles = "Contributor")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name");

            ViewData.Model = new Page { Name = name };
            return View();
        }

        [Authorize(Roles = "Contributor")]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(string name, string title, string contents)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name");

            using (var entities = new KkcDataEntities())
            {
                var page = new Page { Name = name, Title = title, Contents = contents };
                entities.AddToPages(page);
                entities.SaveChanges();

                return RedirectToAction("View", new { name = name });
            }
        }
    }
}
